package com.kaikeba.wx.controller;

import com.kaikeba.bean.BordUserInfo;
import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.RandomUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;
import com.kaikeba.wx.util.IdentityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserController {
    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        //String code = RandomUtil.getCode()+"";    //随机验证码生成
        //boolean flag = SMSUtil.loginSMS(userPhone, code); //短信是否发生成功
        String code = "123456";
        boolean flag = true;
        Message msg = new Message();
        if (flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送");
        }else {
            //短信发送失败
            msg.setStatus(-1);
            msg.setResult("验证码发送失败，请检查手机号码或稍后再试");
        }
        //把验证码保存到session中
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code"); //用户输入的验证码
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone); //发送的验证码
        Message msg = new Message();
        if (sysCode == null){
            //该号码未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获得短信");
        }else if(sysCode.equals(userCode)){
            //手机号码和验证码正确,登录成功
            User user = new User();
            //判断身份
            int identity = IdentityUtil.identity(userPhone);
            if (identity == 1){
                //快递员
                msg.setStatus(1);
                Courier courier = CourierService.findByCourierPhone(userPhone);
                user.setUser(false);
                user.setId(courier.getId());
                user.setNumber(courier.getNumber());
                user.setUsername(courier.getCourierName());
                user.setUserPhone(userPhone);
                user.setLastLoginTime(new Timestamp(System.nanoTime()));
                user.setRegTime(courier.getRegTime());
                user.setPassword(courier.getPassword());
            }else if(identity == 0){
                //用户
                msg.setStatus(0);
                user = UserService.findByUserPhone(userPhone);
                user.setUser(true);
            }else {
                //新用户
                msg.setStatus(-1);
                user.setUserPhone(userPhone);
                user.setUser(true);
            }
            //把user对象保存到session中
            UserUtil.setWxUser(request.getSession(),user);
            msg.setResult("登录成功");
        }else {
            //验证码不一致,登陆失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    //根据是否是快递员来进行相应功能的隐藏
    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        boolean isUser = wxUser.isUser();
        Message msg = new Message();
        if (isUser){
            //是用户
            msg.setStatus(0);
        }else {
            //是管理员
            msg.setStatus(1);
        }
        msg.setResult(wxUser.getUserPhone());
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //销毁session
        request.getSession().invalidate();
        //回复消息
        Message msg = new Message();
        msg.setStatus(0);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/userCheck.do")
    public String userCheck(HttpServletRequest request, HttpServletResponse response){
        //判断是否认证过
        User wxUser = UserUtil.getWxUser(request.getSession());
        Message msg = new Message();
        //姓名为null表示未认证
        if (wxUser==null || wxUser.getUsername()==null){
            //未认证
            msg.setStatus(-1);
            msg.setResult("您还为进行认证，请完善信息");
        }else {
            //已认证
            msg.setStatus(0);
            msg.setResult("已认证");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/authenticationSms.do")
    public String authenticationSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("authenticationPhone");
        //String code = RandomUtil.getCode()+"";    //随机验证码生成
        //boolean flag = SMSUtil.loginSMS(userPhone, code); //短信是否发生成功
        String code = "123456";
        boolean flag = true;
        Message msg = new Message();
        if (flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送");
            //把第二次的验证码保存到session中
            UserUtil.setLoginSms(request.getSession(),userPhone,code);
        }else {
            //短信发送失败
            msg.setStatus(-1);
            msg.setResult("验证码发送失败，请检查手机号码或稍后再试");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/authentication.do")
    public String authenticationLogin(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String number = request.getParameter("number");
        wxUser.setUsername(userName);
        wxUser.setPassword(password);
        wxUser.setNumber(number);
        //把新数据重新存储到Session中
        UserUtil.setWxUser(request.getSession(),wxUser);
        //存到数据库中
        boolean flag = UserService.add(wxUser);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("认证成功!，信息保存成功");
        }else {
            msg.setStatus(1);
            msg.setResult("数据上传失败！请稍后重试");
        }
        return JSONUtil.toJSON(msg);


        /*//得到手机号
        String authenticationPhone = request.getParameter("authenticationPhone");
        //得到用户输入的验证码
        String userCode = request.getParameter("code");
        //通过获得的手机号得到服务器发送的验证码
        String sysCode = UserUtil.getLoginSms(request.getSession(), authenticationPhone);
        Message msg = new Message();
        if (sysCode == null){
            //该手机未接收到验证码(通过该手机找不到验证码) -1
            msg.setStatus(-1);
            msg.setResult("该手机号为获取短信");
        }else if (userCode.equals(sysCode)){
            //验证码一致 0
            //将信息保存至数据库
            //再得到姓名，密码
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            User user = new User(userName,authenticationPhone,password,null);
            user.setUser(true); //是普通用户
            boolean flag = UserService.add(user);
            //把新数据重新存储到Session中
            UserUtil.setWxUser(request.getSession(),user);
            if (flag) {
                msg.setStatus(0);
                msg.setResult("认证成功!，信息保存成功");
            }else {
                msg.setStatus(1);
                msg.setResult("数据上传失败！请稍后重试");
            }
        }else {
            //验证码有误 -2
            msg.setStatus(-2);
            msg.setResult("验证码有误");
        }
        return JSONUtil.toJSON(msg);*/
    }
    @ResponseBody("/wx/getLazyBordInfo.do")
    public String getLazyBordInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String, List<BordUserInfo>> data = new HashMap<>();
        Message msg = new Message();
        /*
        [
        {排名名称,排名List集合}
        {排名名称,排名List集合}
        {排名名称,排名List集合}
        ]
         */
        //得到总排行的数据 rank1
        List<BordUserInfo> rank1 = UserService.getRank();
        data.put("rank1",rank1);
        //得到年排行数据 rank2
        List<BordUserInfo> rank2 = UserService.getRankByYear();
        data.put("rank2",rank2);
        //得到月排行数据 rank3
        List<BordUserInfo> rank3 = UserService.getRankByMonth();
        data.put("rank3",rank3);
        if (rank1.size()>0 && rank2.size()>0 && rank3.size()>0) {
            msg.setStatus(0);
            msg.setResult("数据查询成功");
            msg.setData(data);
        }else {
            msg.setStatus(-1);
            msg.setResult("暂无排名信息");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/getUserHomeInfo.do")
    public String getUserHomeInfo(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String username = wxUser.getUsername();
        Message msg = new Message();
        if (username.isEmpty()){
            msg.setStatus(-1);
            msg.setResult("请尽快完善个人信息");
        }else{
            msg.setStatus(0);
            msg.setResult(username);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/updateSms.do")
    public String updateSms(HttpServletRequest request, HttpServletResponse response){
        String newPhone = request.getParameter("newPhone");
        //String code = RandomUtil.getCode()+"";    //随机验证码生成
        //boolean flag = SMSUtil.loginSMS(userPhone, code); //短信是否发生成功
        String code = "123456";
        boolean flag = true;
        Message msg = new Message();
        if (flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送");
            //把第二次的验证码保存到session中
            UserUtil.setLoginSms(request.getSession(),newPhone,code);
        }else {
            //短信发送失败
            msg.setStatus(-1);
            msg.setResult("验证码发送失败，请检查手机号码或稍后再试");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        //得到手机号
        String newPhone = request.getParameter("newPhone");
        //得到用户输入的验证码
        String userCode = request.getParameter("code");
        //通过获得的手机号得到服务器发送的验证码
        String sysCode = UserUtil.getLoginSms(request.getSession(), newPhone);
        Message msg = new Message();
        if (sysCode == null){
            //该手机未接收到验证码(通过该手机找不到验证码) -1
            msg.setStatus(-1);
            msg.setResult("该手机号为获取短信");
        }else if (userCode.equals(sysCode)){
            //验证码一致 0
            User wxUser = UserUtil.getWxUser(request.getSession());
            String name = request.getParameter("name");
            wxUser.setUsername(name);
            wxUser.setUserPhone(newPhone);
            //把新数据存到数据库中
            boolean flag = UserService.update(wxUser);
            //把新数据重新存储到Session中
            UserUtil.setWxUser(request.getSession(),wxUser);
            if (flag) {
                msg.setStatus(0);
                msg.setResult("认证成功!，信息保存成功");
            }else {
                msg.setStatus(1);
                msg.setResult("数据上传失败！请稍后重试");
            }
        }else {
            //验证码有误 -2
            msg.setStatus(-2);
            msg.setResult("验证码有误");
        }
        return JSONUtil.toJSON(msg);
    }
}
